<?php

declare(strict_types=1);

namespace App\CustHub\Service;

use App\CustHub\Mapper\ChFriendCircleTaskGroupMapper;
use App\CustHub\Mapper\ChFriendCircleTaskMapper;
use Hyperf\Database\Model\Collection;
use Mine\Abstracts\AbstractService;
use Mine\Annotation\Transaction;
use Mine\Exception\NormalStatusException;

class ChFriendCircleTaskGroupService extends AbstractService
{
    public $mapper;

    public function __construct(ChFriendCircleTaskGroupMapper $mapper)
    {
        $this->mapper = $mapper;
    }


    public function list(array $data): Collection|array|\Hyperf\Collection\Collection
    {
        $list = $this->mapper->getGroupList($data);
        return $list;
    }

    public function save(array $data): mixed
    {
        if($this->mapper->checkNameExists($data['name'])){
            throw new NormalStatusException('分组名称重复');
        }
        return $this->mapper->save($data);
    }

    /**
     * @param array $data
     * @return bool
     */
    public function updateGroup(array $data): bool
    {
        if($this->mapper->checkNameExists($data['name'],$data['id'])){
            throw new NormalStatusException('分组名称重复');
        }
        return $this->mapper->update($data['id'],['name'=>$data['name']]);
    }


    /**
     * 删除分组
     * @param array $data
     * @return bool
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[Transaction]
    public function deleteGroup(array $data): bool
    {
        $id =  $data['id'];

        $this->mapper->deleteById($id);
        container()->get(ChFriendCircleTaskMapper::class)->restGroup($id);
        return true;
    }
}